"use strict";
require("test").setup();

var Coroutine = require('coroutine');
var hash = require('hash');

var str_prefix = "YCD" + formatDate(new Date(), "yyyyMMdd");

describe("vehicleRecord", function() {
    before(function() {
        httpPost("GET", "/v1.0/api/user/Sync", {})
        // 用户登录
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2020").digest().hex() + t).digest().hex(),
            "t": t,
        })

        // 创建一个职工
        var result = httpPost("POST", "/v1.0/api/user/Create", {
            "job_number": "001",
            "name": "测试职工一",
            "gender": "男",
            "phone": "18300000002",
            "role": "职工",
            "region": "所在区域",
            "image": "",
            "company": "公司",
            "position": "职务",
        })

        //创建车辆
        result = httpPost("POST", "/v1.0/api/vehicle/Create", {
            "plate_number": "苏A123456",
            "brand": "奥迪",
            "model": "A8",
            "displacement": "2.0",
            "year": "2008",
        })
    });

    after(function() {
        clearDB();
    });

    it("创建用车单", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
            "vehicle_id": 1,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "use_reason": "用车事由",
            "destination": "目的地",
            "address": "目的地地址",
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);

        result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
            "vehicle_id": 1,
            "start_date_time": "2099-12-15 11:00",
            "end_date_time": "2099-12-15 12:00",
            "use_reason": "用车事由",
            "destination": "目的地",
            "address": "目的地地址",
            "reviewed_by": 1,
        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "该时间段车辆已被预约过");

        result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
            "vehicle_id": 1,
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 14:00",
            "use_reason": "用车事由",
            "destination": "目的地",
            "address": "目的地地址",
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 2);
    });

    it("用车单详情", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
        assert.equal(result.data.no, str_prefix + "001");
        assert.equal(result.data.status, "待审核");
        assert.equal(result.data.created_name, "企业管理员");
        assert.equal(result.data.created_phone, "18888888888");
        // assert.equal(result.data.start_date_time, "6天前 10:00");
        // assert.equal(result.data.end_date_time, "6天前 12:00");
        assert.equal(result.data.vehicle_plate_number, "苏A123456");
        assert.equal(result.data.vehicle_model, "A8");
        assert.equal(result.data.vehicle_brand, "奥迪");
        assert.equal(result.data.use_reason, "用车事由");
        assert.equal(result.data.destination, "目的地");
        assert.equal(result.data.address, "目的地地址");
        assert.equal(result.data.reviewed_name, "企业管理员");
        assert.equal(result.data.operated_by, 0);
        assert.equal(result.data.operated_name, "");
    });

    it("编辑用车单 - 改变审批记录中的审核人", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Update", {
            "id": 1,
            "vehicle_id": 1,
            "start_date_time": "2099-12-15 9:00",
            "end_date_time": "2099-12-15 11:00",
            "use_reason": "用车事由2",
            "destination": "目的地2",
            "address": "目的地地址2",
            "reviewed_by": 2,
        })
        assert.equal(result.code, "200");

        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
        // assert.equal(result.data.start_date_time, "8天前 09:00");
        // assert.equal(result.data.end_date_time, "8天前 11:00");
        assert.equal(result.data.vehicle_plate_number, "苏A123456");
        assert.equal(result.data.vehicle_brand, "奥迪");
        assert.equal(result.data.vehicle_model, "A8");
        assert.equal(result.data.vehicle_displacement, "2.0");
        assert.equal(result.data.vehicle_year, "2008");
        assert.equal(result.data.use_reason, "用车事由2");
        assert.equal(result.data.destination, "目的地2");
        assert.equal(result.data.address, "目的地地址2");
        assert.equal(result.data.reviewed_name, "测试职工一");
        assert.equal(result.data.operated_by, 0);
        assert.equal(result.data.operated_name, "");

        // 审批中审批人发生改变,审批列表中记录减少 (不是待我审批的)
        result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "待处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);

        result = httpPost("POST", "/v1.0/api/vehicleRecord/Update", {
            "id": 1,
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 14:00",
        })
        assert.equal(result.msg, "该时间段车辆已被预约过");

        result = httpPost("POST", "/v1.0/api/vehicleRecord/Update", {
            "id": 1,
            "start_date_time": "2099-12-15 15:00",
            "end_date_time": "2099-12-15 14:00",
        })
        assert.equal(result.msg, "用车结束时间必须大于开始时间");

        result = httpPost("POST", "/v1.0/api/vehicleRecord/Update", {
            "id": 1,
            "vehicle_id": 1,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "use_reason": "用车事由",
            "destination": "目的地",
            "address": "目的地地址",
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");

        // 审批中审批人发生改变,审批列表中记录增加 (是待我审批的)
        result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "待处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);
    })

    it("通过用车单", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Pass", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });

    it("已审核的用车单不允许编辑", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Update", {
            "id": 1,

        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "用车预约已通过,不可再次编辑");
    })

    it("拒绝用车单", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Refuse", {
            "id": 2,
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });

    it("撤回用车单", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Withdraw", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.msg, "success");
    });

    it("用车单列表-状态查询", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/List", {
            "status": "全部",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);

        result = httpPost("POST", "/v1.0/api/vehicleRecord/List", {
            "status": "已撤回",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.list[0].operated_by, 1);
        assert.equal(result.data.list[0].operated_name, "企业管理员");

        result = httpPost("POST", "/v1.0/api/vehicleRecord/List", {
            "status": "已通过",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);
    });

    it("用车单列表-申请人模糊查询", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/List", {
            "created_name": "企业",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);

        var result = httpPost("POST", "/v1.0/api/vehicleRecord/List", {
            "created_name": "啦啦啦",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);
    });

    it("用车单列表-申请人模糊查询-统计", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Static", {
            "created_name": "企业",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.total, 2);
        assert.equal(result.data.unreviewed, 0);
        assert.equal(result.data.passed, 0);
        assert.equal(result.data.refused, 1);
        assert.equal(result.data.withdrawed, 1);

        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Static", {
            "created_name": "啦啦啦",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.total, 0);
        assert.equal(result.data.unreviewed, 0);
        assert.equal(result.data.passed, 0);
        assert.equal(result.data.refused, 0);
        assert.equal(result.data.withdrawed, 0);
    });

    it("用车单列表-时间区间查询", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/List", {
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 15:00",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);

        result = httpPost("POST", "/v1.0/api/vehicleRecord/List", {
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 15:00",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
    });

    it("公务车管理-用车记录-车辆ID查询", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/List", {
            "vehicle_id": 1,
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);

        result = httpPost("POST", "/v1.0/api/vehicleRecord/List", {
            "vehicle_id": 2,
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);
    });

    it("创建/编辑用车单-选择所需车辆", function() {
        // 占用车辆
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
            "vehicle_id": 1,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "use_reason": "用车事由",
            "destination": "目的地",
            "address": "目的地地址",
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 3);

        // 创建时-选择车辆结果

        result = httpPost("POST", "/v1.0/api/vehicle/EffectiveList", {
            "start_date_time": "2099-12-15 11:00",
            "end_date_time": "2099-12-15 12:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);

        result = httpPost("POST", "/v1.0/api/vehicle/EffectiveList", {
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 13:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);

        result = httpPost("POST", "/v1.0/api/vehicle/EffectiveList", {
            "start_date_time": "2099-12-15 15:00",
            "end_date_time": "2099-12-15 14:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);

        // 编辑时-选择车辆结果 (排除当前用车单)

        result = httpPost("POST", "/v1.0/api/vehicle/EffectiveList", {
            "vehicle_record_id": 3,
            "start_date_time": "2099-12-15 11:00",
            "end_date_time": "2099-12-15 12:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);

        result = httpPost("POST", "/v1.0/api/vehicle/EffectiveList", {
            "vehicle_record_id": 3,
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 13:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);

        result = httpPost("POST", "/v1.0/api/vehicle/EffectiveList", {
            "vehicle_record_id": 3,
            "start_date_time": "2099-12-15 15:00",
            "end_date_time": "2099-12-15 14:00",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);

    });

    // it("过期预约-不可审核", function() {
    //     var result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
    //         "vehicle_id": 1,
    //         "start_date_time": "2009-12-15 10:00",
    //         "end_date_time": "2009-12-15 12:00",
    //         "use_reason": "用车事由",
    //         "destination": "目的地",
    //         "address": "目的地地址",
    //         "reviewed_by": 1,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.id, 4);

    //     result = httpPost("POST", "/v1.0/api/vehicleRecord/Get", {
    //         "id": 4,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.id, 4);
    //     assert.equal(result.data.status, "待审核");

    //     result = httpPost("POST", "/v1.0/api/vehicleRecord/Pass", {
    //         "id": 4
    //     })
    //     assert.equal(result.code, "400");
    //     assert.equal(result.msg, "用车预约已过期");

    //     result = httpPost("POST", "/v1.0/api/vehicleRecord/Refuse", {
    //         "id": 4
    //     })
    //     assert.equal(result.code, "400");
    //     assert.equal(result.msg, "用车预约已过期");
    // });

});

require('test').run();